﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Reflection;

namespace Pixysoft.IO
{
    public class ImbeddedReader
    {
        private static volatile ImbeddedReader instance;

        private static object syncRoot = new Object();

        public static ImbeddedReader Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (syncRoot)
                    {
                        instance = new ImbeddedReader();
                    }
                }

                return instance;

            }
        }

        /// <summary>
        /// 直接反射成对象 对象的存储路径=handle.Namespace + filename
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="filename"></param>
        /// <returns></returns>
        public T Import<T>(object handle, string filename)
        {
            return XmlImporter.Instance.XmlDeserialize<T>(
                GetResource(handle, filename));
        }


        public string GetResource<T>(string filename)
        {
            Stream stream = GetResourceStream(typeof(T), filename);

            if (stream == null)
                return null;

            StreamReader reader = new StreamReader(stream);

            string schema = reader.ReadToEnd();

            reader.Dispose();

            return schema;
        }

        /// <summary>
        /// 获取资源内容 对象的存储路径=handle.Namespace + filename
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        public string GetResource(object handle, string filename)
        {
            Stream stream = GetResourceStream(handle, filename);

            if (stream == null)
                return null;

            StreamReader reader = new StreamReader(stream);

            string schema = reader.ReadToEnd();

            reader.Dispose();

            return schema;
        }

        /// <summary>
        /// 获取byte资源
        /// </summary>
        /// <param name="handler"></param>
        /// <param name="filename"></param>
        /// <returns></returns>
        public byte[] GetResourceBytes(object handle, string filename)
        {
            Stream stream = GetResourceStream(handle, filename);

            if (stream == null)
                return null;

            return StreamCoder.Instance.ToBytes(stream);
        }

        /// <summary>
        /// 获取资源stream，filename的文件夹以“。”分割 对象的存储路径=handle.Namespace + filename
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        public Stream GetResourceStream(object handle, string filename)
        {
            if (handle == null)
                return null;

            return GetResourceStream(handle.GetType(), filename);
        }

        public Stream GetResourceStream(Type handleType, string filename)
        {
            if (handleType == null)
                return null;

            if (string.IsNullOrEmpty(filename))
                return null;

            //获得正在运行类所在的名称空间

            string _namespace = handleType.Namespace;

            //获得当前运行的Assembly

            Assembly _assembly = Assembly.GetAssembly(handleType);//.GetExecutingAssembly();

            //根据名称空间和文件名生成资源名称

            string resourceName = _namespace + "." + filename;

            //根据资源名称从Assembly中获取此资源的Stream

            Stream stream = _assembly.GetManifestResourceStream(resourceName);

            return stream;
        }
    }
}
